﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Configuration;

namespace com.tiyma.components
{
    public class AppConfig
    {
        /// <summary>
        /// sql连接字符串
        /// </summary>
        public static string SqlConnectionString
        {
            get { return GetConfig("SqlConnectionString", @"Data Source=.;Initial Catalog=Book;User ID=sa;Password=123456"); }
        }

        /// <summary>
        /// 索引文件路径根目录
        /// </summary>
        public static string BookIndexPath
        { 
            get 
            {
                string t_book_index_path= GetConfig("bookIndexPath", "");
                if (!Directory.Exists(t_book_index_path))
                {
                    Directory.CreateDirectory(t_book_index_path);
                }

                return t_book_index_path;
            } 
        }

        /// <summary>
        /// 索引临时目录
        /// </summary>
        public static string BookIndexPathTemp
        {
            get
            {
                string t_book_index_path_temp= GetConfig("BookIndexPathTemp", "");
                if (!Directory.Exists(t_book_index_path_temp))
                {
                    Directory.CreateDirectory(t_book_index_path_temp);
                }

                return t_book_index_path_temp;
            } 
        }

        /// <summary>
        /// js文件路径
        /// </summary>
        public static string BookJSPath
        {
            get
            {
                string t_book_js_path= GetConfig("bookJSPath", "");
                if (!Directory.Exists(t_book_js_path))
                    Directory.CreateDirectory(t_book_js_path);

                return t_book_js_path;
            }
        }

        /// <summary>
        /// 收录小说程序路径
        /// </summary>
        public static string CrawlerAppPath
        {
            get
            {
                return GetConfig("CrawlerAppPath", "");
            }
        }

        /// <summary>
        /// 站点地图路径
        /// </summary>
        public static string SiteMapPath
        {
            get
            {
                string t_site_map_path = GetConfig("SiteMapPath", "");
                if (!Directory.Exists(t_site_map_path))
                    Directory.CreateDirectory(t_site_map_path);

                return t_site_map_path;
            }
        }

        /// <summary>
        /// 重建索引的时间间隔 单位小时 默认8小时一次
        /// </summary>
        public static int BookIndexInterval
        {
            get
            {
                int t_index_interval = 0;
                if (!int.TryParse(GetConfig("BookIndexInterval", "8"), out t_index_interval))
                    t_index_interval = 0;

                return t_index_interval;
            }
        }

        /// <summary>
        /// MySQL数据库链接
        /// </summary>
        public static string MySQLConnectionString
        {
            get { return GetConfig("MySQLConnectionString", @"server=127.0.0.1;user id=root; password=tyh2004111091; database=mysql"); }
        }

        /// <summary>
        /// 数据库名字
        /// </summary>
        public static string DbName
        {
            get { return GetConfig("DbName", @"tiyma"); }
        }

        /// <summary>
        /// 最新更新的小说数量
        /// </summary>
        public static int UpdateBookCount
        {
            get
            {
                int t_count = 50;
                if (!int.TryParse(GetConfig("UpdateBookCount", @"50"), out t_count))
                    t_count = 50;
                return t_count;
            }
        }

        /// <summary>
        /// 服务名
        /// </summary>
        public static string ServiceName
        {
            get
            {
                return GetConfig("ServiceName", "tiyma_seo_service");
            }
        }

        /// <summary>
        /// 服务名
        /// </summary>
        public static string WithchServiceName
        {
            get
            {
                return GetConfig("WitchServiceName", "tiyma_crawler");
            }
        }

        

        #region tools
        private static NameValueCollection _settings = null;

        public static NameValueCollection Settings
        {
            get { return AppConfig._settings; }
            set { AppConfig._settings = value; }
        }



        private static string GetConfig(string key, string defaultValue)
        {

            if (_settings == null)
                _settings = ConfigurationManager.AppSettings;

            if (Settings[key] != null)
                return Settings[key];
            else
                return defaultValue;

        }
        #endregion
    }
}
